package carddeck;

import java.util.LinkedList;
import java.util.Random;

import GUI.gameWinOld.CardPanelInterface;


public class CardDeck {
	private LinkedList<Card> cardDeck;
	private CardPanelInterface cardPanel;
	private static int jkrPerDeck=3;
	public CardDeck() {
		cardDeck = new LinkedList<Card>();
	}
	
	public CardPanelInterface getCardPanel() {
		return cardPanel;
	}

	public void setCardPanel(CardPanelInterface cardPanel) {
		this.cardPanel = cardPanel;
		cardPanel.refresh();
	}

	public void reset() {
		cardDeck.clear();
	}
	
	public void addNewDeck() {
		Suit theSuit = Suit.HEARTS;
		while (true) {
			cardDeck.add(new Card(theSuit, Rank.ACE));
			cardDeck.add(new Card(theSuit, Rank.TWO));
			cardDeck.add(new Card(theSuit, Rank.THREE));
			cardDeck.add(new Card(theSuit, Rank.FOUR));
			cardDeck.add(new Card(theSuit, Rank.FIVE));
			cardDeck.add(new Card(theSuit, Rank.SIX));
			cardDeck.add(new Card(theSuit, Rank.SEVEN));
			cardDeck.add(new Card(theSuit, Rank.EIGHT));
			cardDeck.add(new Card(theSuit, Rank.NINE));
			cardDeck.add(new Card(theSuit, Rank.TEN));
			cardDeck.add(new Card(theSuit, Rank.JACK));
			cardDeck.add(new Card(theSuit, Rank.QUEEN));
			cardDeck.add(new Card(theSuit, Rank.KING));
			if (theSuit == Suit.HEARTS)
				theSuit = Suit.DIAMONDS;
			else if (theSuit == Suit.DIAMONDS)
				theSuit = Suit.CLUBS;
			else if (theSuit == Suit.CLUBS)
				theSuit = Suit.SPADES;
			else
				break;
		}
		for(int i=0;i<jkrPerDeck;i++) {
			cardDeck.add(new Card(Suit.UNDEFINED, Rank.UNDEFINED));
		}
		shuffleDeck();
		if(cardPanel!=null)
			cardPanel.refresh();
	}

	public int getCardCount() {
		return cardDeck.size();
	}

	public Card peekCard(int index) {
		if (index >= cardDeck.size())
			return null;
		return cardDeck.get(index);
	}

	public Card peekFirstCard() {
		return peekCard(0);
	}

	public Card getCard(int index) {
		if (index >= cardDeck.size())
			return null;
		Card theCard = cardDeck.get(index);
		cardDeck.remove(index);
		cardPanel.refresh();
		return theCard;
	}

	public Card getFirstCard() {
		cardPanel.refresh();
		return getCard(0);
	}
	
	public Card peekLastCard() {
		return peekCard(cardDeck.size()-1);
	}
	
	public Card getLastCard() {
		cardPanel.refresh();
		return getCard(cardDeck.size()-1);
	}
	
	public LinkedList<Card> getCards() {
		return cardDeck;
	}
	
	public void shuffleDeck() {
		LinkedList<Card> tempDeck = new LinkedList<Card>();
		Random generator = new Random();
		while (!cardDeck.isEmpty()) {
			int index = generator.nextInt(cardDeck.size());
			tempDeck.add(cardDeck.get(index));
			cardDeck.remove(index);
		}
		cardDeck = tempDeck;
	}
	
	public void appendCards(LinkedList<Card> cards) {
		for(Card theCard : cards)
			cardDeck.add(theCard);
		cardPanel.refresh();
	}
	
	public void addCard(Card theCard) {
		cardDeck.add(theCard);
		cardPanel.refresh();
	}

	public static int getJkrPerDeck() {
		return jkrPerDeck;
	}

	public static void setJkrPerDeck(int jkrPerDeck) {
		CardDeck.jkrPerDeck = jkrPerDeck;
	}
}
